
if exists (select 1 from sysobjects where name = 'get_plantermine' and type = 'P')
begin
   drop procedure get_plantermine
   print 'Procedure: get_plantermine deleted ...'
end
go
create procedure get_plantermine(
  @planid             int               = 25,
  @mandid             char(2)           = 'li',
  @planterminid       int               = 0
)
as
begin
  set nocount on

  select terminid = tp.TerminID,
         terminname = tp.Name,
         planid = tp.PlanID, 
         prsid = tp.PrsID, 
         datum = tp.Datum, 
         abteilungid = tp.AbteilungID
    from Terminplan tp 
   inner join Termin t 
      on tp.TerminID = t.TerminID 
   where tp.PlanID = @planid 
     and (tp.PlanTerminID = @planterminid or @planterminid = 0) 
     and t.mandid = @mandid
   order by tp.TerminID, tp.PrsID, tp.AbteilungID

end
go
print 'Procedure: get_plantermine done ...'
go
grant exec on get_plantermine to prsadmins with grant option
go
grant exec on get_plantermine to prsusers
go

